﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX.DirectInput;


namespace RescueApplication
{
    public partial class joyStick
    {
        #region Varible #falcon_best
        private Device joystickDevice;
        private JoystickState state;
        
        private IntPtr hWnd;
        private string[] systemJoysticks;
       
        #endregion

        public joyStick(IntPtr hW)
        {
            hWnd = hW;
        }
        
        private int[] joyAnalogButton = new int[10];
        public int[] JoyAnalogButtons
        {
            get { return joyAnalogButton; }
        }

        private int pov;
        public int Pov
        {
            get { return pov; }
        }

        private bool[] buttons;
        public bool[] Buttons
        {
            get { return buttons; }
        }

        private void Poll()
        {
            try
            {
                joystickDevice.Poll();
                state = joystickDevice.CurrentJoystickState;
            }
            catch (Exception err)
            {
                MessageBox.Show("Error Poll : \n" + err.Message);
                ReleaseJoystick();
            }
        }

        public string[] FindJoysticks()
        {
            systemJoysticks = null;

            try
            {
                DeviceList gameControllerList = Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly);
                if (gameControllerList.Count > 0)
                {
                    systemJoysticks = new string[gameControllerList.Count];
                    int i = 0;
                    foreach (DeviceInstance deviceInstance in gameControllerList)
                    {
                        
                        joystickDevice = new Device(deviceInstance.InstanceGuid);
                        joystickDevice.SetCooperativeLevel(hWnd,
                            CooperativeLevelFlags.Background |
                            CooperativeLevelFlags.NonExclusive);

                        systemJoysticks[i] = joystickDevice.DeviceInformation.InstanceName;

                        i++;
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show("Error Findjoy : \n" + err.Message);
            }

            return systemJoysticks;
        }

        public bool AcquireJoystick(string name)
        {
            try
            {
                DeviceList gameControllerList = Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly);
                int i = 0;
                bool found = false;

                foreach (DeviceInstance deviceInstance in gameControllerList)
                {
                    if (deviceInstance.InstanceName == name)
                    {
                        found = true;
                        joystickDevice = new Device(deviceInstance.InstanceGuid);
                        joystickDevice.SetCooperativeLevel(hWnd,
                            CooperativeLevelFlags.Background |
                            CooperativeLevelFlags.NonExclusive);
                        break;
                    }

                    i++;
                }

                if (!found)
                    return false;

                joystickDevice.SetDataFormat(DeviceDataFormat.Joystick);


                joystickDevice.Acquire();

                DeviceCaps cps = joystickDevice.Caps;
                //txtReport.Text += cps.NumberPointOfViews.ToString() + "-";
                //txtReport.Text += cps.NumberButtons.ToString() + "-";
                //txtReport.Text += cps.NumberAxes.ToString() + "-";

                UpdateStatus();
            }
            catch (Exception err)
            {
                MessageBox.Show("Find joy Error : \n" + err.Message);
                return false;
            }

            return true;
        }

        public void ReleaseJoystick()
        {
            joystickDevice.Unacquire();
        }

        public bool UpdateStatus()
        {
            Poll();
            
            int[] extraAxis = state.GetPointOfView();
            byte[] jsButtons = state.GetButtons();
            buttons = new bool[jsButtons.Length];

            int i = 0;
            foreach (byte button in jsButtons)
            {
                buttons[i] = button >= 128;
                i++;
            }

            #region Button and POV
            joyAnalogButton[2] = state.Z;
            joyAnalogButton[0] = state.X;
            joyAnalogButton[1] = state.Y;
            joyAnalogButton[3] = state.Rx;
            joyAnalogButton[4] = state.Ry;

            pov = extraAxis[0];
            #endregion
            return true;
        }


    }
}
